package com.ruoyi.system.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.entity.Employee;
import com.ruoyi.system.mapper.EmployeeMapper;
import com.ruoyi.system.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController extends BaseController {

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private EmployeeMapper employeeMapper;

    @GetMapping("/list")
    public AjaxResult list(Employee employee) {
        List<Employee> list = employeeService.list(Wrappers.query(employee));
        return AjaxResult.success(list);
    }

    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        Employee employee = employeeService.getWithServiceRecords(id);
        return AjaxResult.success(employee);
    }

    @PostMapping
    public AjaxResult add(@RequestBody Employee employee) {
        employeeService.save(employee);
        return AjaxResult.success();
    }

    @PutMapping
    public AjaxResult edit(@RequestBody Employee employee) {
        employeeService.updateById(employee);
        return AjaxResult.success();
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        //employeeService.removeById(id);
        employeeMapper.deleteById(id);
        return AjaxResult.success();
    }
}